package cn.edu.ctbu.myfirstweb.webapi;

import cn.edu.ctbu.myfirstweb.domain.*;
import cn.edu.ctbu.myfirstweb.helper.R;
import cn.edu.ctbu.myfirstweb.helper.RUtil;
import cn.edu.ctbu.myfirstweb.service.SCService;
import cn.edu.ctbu.myfirstweb.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/webapi/teacher")
public class TeacherRestController {
    @Autowired
    TeacherService teacherService;

    @Qualifier("SCImpService")
    @Autowired
    private SCService scService;

   /*.**
   * @Description: 获取所有教师数据
   * @Param:  void
   * @Return: List<Teacher>
   * @Author: HighJ
   * @Date Created in
   */

    @GetMapping("/list")
    public List<Teacher> getAll() {
        return teacherService.findAll();
    }
/**.**
* @Description: 插入新的教师
* @Param:  Teacher
* @Return: Teacher
* @Author: HighJ
* @Date Created in
*/

    @PostMapping("/insert")
    public Teacher insert(Teacher teacher) {
        return teacherService.insert(teacher);
    }
/**
* @Description: 更新教师数据
* @Param:  Teacher
* @Return: Teacher
* @Author: HighJ
* @Date Created in
*/
    @PutMapping("/update")
    public Teacher update(Teacher teacher) {
        Teacher oldteacher = teacherService.query(teacher.getTid());
        if (StringUtils.isEmpty(teacher.getPassword())) {
            teacher.setPassword(oldteacher.getPassword());
        }
        teacher = teacherService.update(teacher);
        return teacher;
    }
/*.**
* @Description:通过tid删除指定教师
* @Param:  tid
* @Return: void
* @Author: HighJ
* @Date Created in
*/

    @DeleteMapping("/delete/{tid}")
    public void delete(@PathVariable Long tid) {
        teacherService.deleteByTid(tid);
    }
/*.**
* @Description: 通过tid获取教师对象
* @Param:  tid
* @Return: Teacher
* @Author: HighJ
* @Date Created in
*/

    @GetMapping("/getbytid/{tid}")
    public Teacher getbyid(@PathVariable Long tid) {
        return teacherService.query(tid);
    }

    @GetMapping("/getbypage")
    public R<Student> getByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                @RequestParam(value = "size", defaultValue = "3") Integer size,
                                @RequestParam(value = "name", defaultValue = "") String name) {
        /*新版本sort已不可实例化，只能通过sort.by来获取，同时此处的pageale对象已经变为protected的了，现在可通过Pageable.of方法构造*/
        Page<Teacher> teacherPage;
        if (StringUtils.isEmpty(name)) {
            Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "tid"));
            teacherPage = teacherService.findAll(pageable);
        } else {
            Teacher teacher = new Teacher();
            teacher.setName(name);//创建包含了按例查询字段的实例对象
            Pageable pageable = PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "tid"));//设置分页数据
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());//该类用于设置匹配条件
            Example<Teacher> example = Example.of(teacher, matcher);//创建example对象，即创建按例查询对象（包含查询字段的实例对象，匹配条件）
            teacherPage = teacherService.findAll(example, pageable);
        }
        return RUtil.success(teacherPage);
    }


   /*.**
   * @Description: 获取教师对象的所有课程信息
   * @Param:  request
   * @Return: List<Course>
   * @Author: HighJ
   * @Date Created in
   */

    @GetMapping("/getcourse")
    public R<List<Course>> getcourse(HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("SYSTEM_USER_SESSION");
        List<Course> courses = teacherService.getCourseByTid(teacher.getTid());
        if (courses != null) {
            return RUtil.success(courses);
        }
        return RUtil.error(0, "该教师无课程或查询错误！");
    }

   /*.**
   * @Description: 获取教师所授某一课程的学生成绩信息
   * @Param:  cid
   * @Return: List<StudentScore>
   * @Author: HighJ
   * @Date Created in
   */

    @GetMapping("/getgrade/{cid}")
    public R<List<StudentScore>> getgrade(@PathVariable Long cid) {
        List<SC> scs = scService.queryByCID(cid);
        if (scs != null) {
            return RUtil.success(scs);
        }
        return RUtil.error(0, "查询错误！");
    }

    /**
     * 更新SC表中的成绩信息
     *
     * @param sid
     * @param cid
     * @param score
     * @return
     */
    @GetMapping("/updatesc")
    public R<SC> updatesc(String sid, String cid, String score) {
        SCID scid = new SCID(Long.parseLong(sid), Long.parseLong(cid));
        float Score = Float.parseFloat(score);
        SC sc = scService.getBySCID(scid);
        if (sc != null) {
            sc.setScore(Score);
            sc = scService.update(sc);
            return RUtil.success(sc);
        }
        return RUtil.error(0, "error");
    }

 /*.**
 * @Description: 通过sid与cid1获取scid对象
 * @Param:  sid, cid1
 * @Return: scid
 * @Author: HighJ
 * @Date Created in
 */

    @GetMapping("/getsc")
    public R<SC> getsc(String sid, String cid1) {
        SCID scid = new SCID(Long.parseLong(sid), Long.parseLong(cid1));
        SC sc = scService.getBySCID(scid);
        if (sc != null) {
            return RUtil.success(sc);
        }

        return RUtil.error(0, "SCID错误");
    }
}
